package org.numanint.code;


import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;




public class ReverseEngineer {

	private TableClass[] tableClasss;
	private Connection conn;
	private String schema;
	public String getSchema() {
		return schema;
	}

	public void setSchema(String schema) {
		this.schema = schema;
	}
	
	private String[] tableNames;
	
	public TableClass[] getTableClasss() {
		return tableClasss;
	}

	public Connection getConn() {
		return conn;
	}

	public String[] getTableNames() {
		return tableNames;
	}
	
	public void setTableClasss(TableClass[] tableClasss) {
		this.tableClasss = tableClasss;
	}

	public void setConn(Connection conn) {
		this.conn = conn;
	}

	public void setTableNames(String[] tableNames) {
		this.tableNames = tableNames;
	}
	
	public ReverseEngineer(Connection conn,String[] tableNames,String schema){
		this.conn = conn;
		this.tableNames = tableNames;
		this.schema = schema;
	}
	
	public void reverse() throws Exception{
		ResultSet rs = null;
		tableClasss = new TableClass[tableNames.length];
		for(int i=0;i<tableNames.length;i++){
			tableClasss[i] = new TableClass();
			tableClasss[i].setTableName(tableNames[i]);
			tableClasss[i].setTableClassName(ConventUtils.t2c(tableNames[i]));
			//获取主键
		    rs = conn.getMetaData().getPrimaryKeys(null,schema,tableNames[i]);		   
			while(rs.next())
				tableClasss[i].getPkArray().add(new TablePKClass(rs.getString("COLUMN_NAME"),ConventUtils.t2c(rs.getString("COLUMN_NAME"))));//获得主键，可能是混合主键
			rs.close();
			//获取导出键
			rs = conn.getMetaData().getExportedKeys(null, schema, tableNames[i]);
		    while (rs.next()) {
		    	String keyTableName = rs.getString("FKTABLE_NAME");
		    	String keyColumnName = rs.getString("FKCOLUMN_NAME");
		    	tableClasss[i].getExportKeyArray().add(new TableKeyClass(keyTableName,keyColumnName,ConventUtils.t2c(keyTableName),ConventUtils.t2c(keyColumnName)));		    	
		     }
		    rs.close();
		    //获取导入键
			rs = conn.getMetaData().getImportedKeys(null, schema, tableNames[i]);
		    while (rs.next()) {
		    	String keyTableName = rs.getString("PKTABLE_NAME");
		    	String keyColumnName = rs.getString("PKCOLUMN_NAME");
		    	tableClasss[i].getImportKeyArray().add(new TableKeyClass(keyTableName,keyColumnName,ConventUtils.t2c(keyTableName),ConventUtils.t2c(keyColumnName)));		    	
		     }
		    rs.close();
		    //获取列信息
		 
		    rs = conn.getMetaData().getColumns(null,schema, tableNames[i],"%"); 
		    ArrayList<TableFieldClass> fieldArray = new ArrayList<TableFieldClass>();
		    while(rs.next()){
		    	TableFieldClass field = new TableFieldClass();
		    	field.setName(rs.getString("COLUMN_NAME"));
		    	field.setType(rs.getString("TYPE_NAME"));	
		    	//System.out.println(field.getType()+java.sql.Types.);
		    	field.setClassName(ConventUtils.t2c(rs.getString("COLUMN_NAME")));
		    	field.setClassType(ConventUtils.typeFmt(rs.getString("TYPE_NAME")));		    	
		    	field.setLength(rs.getInt("COLUMN_SIZE"));
		    	fieldArray.add(field);
		    }
		    tableClasss[i].setFields(fieldArray.toArray(new TableFieldClass[fieldArray.size()]));
		    rs.close();
		}		
		conn.close();
       
	}
}
